package com.ncmmall.application.business.subject;

import cn.hutool.core.util.ZipUtil;
import com.feijin.commons.domain.Ids;
import com.feijin.commons.lang.PropertiesHelps;
import com.ncmmall.domain.business.subject.SubjectActivity;
import com.ncmmall.domain.business.subject.SubjectActivityRepository;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;

/**
 * 专题活动管理
 */
@Service
@Transactional
public class SubjectActivityApplication {

    @Autowired
    private SubjectActivityRepository subjectActivityRepository;

    private final String storePath = PropertiesHelps.getValue("store.path");

    public void create(SubjectActivity params) throws IOException {

        params.setCreateTime(new Timestamp(System.currentTimeMillis()));
        params.setStatus(1);

        String randDir = Ids.generateId();
        String pathDir = storePath + "/subject/" + randDir;
        unzip(storePath + "/uploads/" + params.getTemplateRealName(), pathDir);

        params.setTemplatePath("/subject/" + randDir);

        subjectActivityRepository.save(params);
    }

    /**
     * 解压zip文件到制定文件夹
     *
     * @param zipFile 压缩文件
     * @param outDir  输出目录
     */
    private void unzip(String zipFile, String outDir) {
        ZipUtil.unzip(zipFile, outDir);
    }

    public void enable(SubjectActivity subjectActivity) {
        subjectActivity.setStatus(2);
        subjectActivityRepository.save(subjectActivity);
    }

    public void disable(SubjectActivity subjectActivity) {
        subjectActivity.setStatus(1);
        subjectActivityRepository.save(subjectActivity);
    }

    public void remove(SubjectActivity subjectActivity) {
        subjectActivityRepository.delete(subjectActivity);
    }

    /**
     * 编辑专题
     */
    public void modify(SubjectActivity params, SubjectActivity subjectActivity) throws IOException {

        subjectActivity.setType(params.getType());
        subjectActivity.setName(params.getName());
        subjectActivity.setDescription(params.getDescription());

        if (!params.getTemplateRealName().equals(subjectActivity.getTemplateRealName())) {
            String randDir = Ids.generateId();
            String pathDir = storePath + "/subject/" + randDir;
            unzip(storePath + "/uploads/" + params.getTemplateRealName(), pathDir);

            //如果压缩包发生变化，删除原压缩包以及压缩文件夹 删除错误不抛出异常
            FileUtils.deleteQuietly(new File(storePath + subjectActivity.getTemplatePath()));
            FileUtils.deleteQuietly(new File(storePath + "/uploads/" + subjectActivity.getTemplateRealName()));

            subjectActivity.setTemplateName(params.getTemplateName());
            subjectActivity.setTemplateRealName(params.getTemplateRealName());
            subjectActivity.setTemplatePath("/subject/" + randDir);
        }

        subjectActivityRepository.save(subjectActivity);
    }
}
